
//
//  DiscoverMCircleDetailViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "HouseholdDetailViewController.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "HttpRequest.h"
#import "TheLoginViewController.h"
#import "UIButton+WebCache.h"
#import "KeyboardModel.h"
#import "ShareView.h"
#import "DiscoverHouseholdCommentsViewController.h"
#import "ToBindCompanyViewController.h"
#import "NewMediaPromotionViewController.h"
@interface HouseholdDetailViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextViewDelegate, UIWebViewDelegate,KeyboardModelDelegate,ShareViewDelegate>

@end

@implementation HouseholdDetailViewController
{
    ShareView*SV;
    UIView*shareblackview;
    UIScrollView*MYmainScroll;
    UIWebView*webview;
    NSString*isCollect;
    NSDictionary*dic;
}
@synthesize alt = alt;
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self closeKeyBoad];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.navigationController.delegate = self;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    UIButton* sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [sxBtn setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    self.view.backgroundColor=[UIColor whiteColor];
    [self setup];
    
}

-(void)closeKeyBoad
{
    [self.window makeKeyWindow];
    self.window.hidden=YES;
    [SV removeFromSuperview];
   
}
-(void)cancel:(NSString *)cancel{
    [self closeKeyBoad];
}

-(void)share:(UIButton*)sender{
   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSString* companyId = [user objectForKey:@"companyId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"Household" forKey:@"contentType"];
    [param setValue:self.fid forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [param setValue:companyId forKey:@"companyId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Company/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                [self.window makeKeyAndVisible];
                
                shareblackview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                shareblackview.backgroundColor=[UIColor blackColor];
                shareblackview.alpha=0.65;
               [self.window addSubview:shareblackview];

                SV = [[ShareView alloc]initWithFrame:CGRectMake(0, ScreenHeight-245, ScreenWidth,245)];
                SV.dataDict=severDataDict.dataDict;

                SV.mDelegate=self;
                [self.window addSubview:SV];
                
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [shareblackview addGestureRecognizer:singleFingerOne];

                
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==8888) {
                    ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
                    [self.navigationController pushViewController:Login animated:YES];}
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==4444) {
                      [self showAlertMessage:@"账号信息未认证通过，请从新上传"];
                    ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
                    [self.navigationController pushViewController:Login animated:YES];

                }
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    }
-(void)successfullyReturns:(NSString *)successfullyReturns{//分享成功后回调登记接口
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"Household" forKey:@"contentType"];
    [param setValue:self.fid forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Company/ShareRecords/Add" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [self closeKeyBoad];
                NewMediaPromotionViewController *loginVC=[[NewMediaPromotionViewController alloc] init];
                [self.navigationController pushViewController:loginVC animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";

    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    [webview removeFromSuperview];
    webview  = [[UIWebView   alloc]  initWithFrame:CGRectMake( 0, 0,  ScreenWidth ,  ScreenHeight)];
    webview.scalesPageToFit = TRUE;
    [webview   setUserInteractionEnabled: YES ];	 //是否支持交互
    [webview   setDelegate: self ];				 //委托
    [webview   setOpaque: YES ];
    webview.backgroundColor=[UIColor clearColor];
    [self.view  addSubview : webview];

}

- (void)setup{
    
    
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    [HttpRequest requestData:@"/Discover/Household/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
        
                 NSLog(@"-----%@",severDataDict.dataDict);
                NSString * path =[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"infoPage"]];
                [webview loadRequest:[NSURLRequest requestWithURL:[[NSURL alloc] initWithString :path]]];
                isCollect=[severDataDict.dataDict objectForKey:@"isCollect"] ;
                dic=severDataDict.dataDict;
                [[KeyboardModelboard customKeyboard]textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isCollect"] pinglunshuString1 :[[severDataDict.dataDict objectForKey:@"householdInfo"]objectForKey:@"commentCount"]   customKeyboardDelegate:self];
                }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        }];
}
-(void)pinglunBtn:(UITextField *)textViewGet
{
    NSLog(@"%@",textViewGet.text);
    if (textViewGet.text.length==0)
    {
        
        [self showAlertMessage:@"内容为空"];
    }else{
        if ([textViewGet.text isEqualToString:@"我来说两句..."])
        {
            [self showAlertMessage:@"内容为空"];
        }else{
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            NSString* tokenId = [user1 objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:tokenId forKey:@"tokenId"];
            [params setValue:self.fid forKey:@"infoId"];
            [params setValue:textViewGet.text forKey:@"content"];
            NSLog(@"-----%@",params);
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:10 view:self.view];
            [HttpRequest requestData:@"/Discover/Household/AddComment" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    [Global hideProgressHUDInternal:self.view];
                    NSLog(@"-----%@",severDataDict.dataDict);
                    textViewGet.text=@"";
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        DiscoverHouseholdCommentsViewController*detailViewController = [[DiscoverHouseholdCommentsViewController alloc] init];
                        detailViewController.fid=self.fid;
                        detailViewController.isCollect=isCollect;
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==8888) {
                            ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
                            [self.navigationController pushViewController:Login animated:YES];
                            }
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==4444) {
                            [self showAlertMessage:@"账号信息未认证通过，请从新上传"];
                            ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
                            [self.navigationController pushViewController:Login animated:YES];
                        }}
                    }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];}
        
    }
}
-(void)pinglunBtnjiemian:(NSString *)pinglunjiemian{
   
    DiscoverHouseholdCommentsViewController*detailViewController = [[DiscoverHouseholdCommentsViewController alloc] init];
    detailViewController.fid=self.fid;
      detailViewController.isCollect=isCollect;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)guangzhuBtn:(UIButton *)guanzhu
{
    NSLog(@"%@",guanzhu.titleLabel.text);
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:10 view:self.view];
    [HttpRequest requestData:@"/Discover/Household/Collect" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            [Global hideProgressHUDInternal:self.view];
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
            
                [[KeyboardModelboard customKeyboard]textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isCollect"] pinglunshuString1 :[[dic objectForKey:@"householdInfo"]objectForKey:@"commentCount"]    customKeyboardDelegate:self];
                    isCollect=[severDataDict.dataDict objectForKey:@"isCollect"] ;
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];}}
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end
